/**
 * 
 */
package com.livemeter;

import com.livemeter.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;


/**
 *
 *Activity to show/activate available polls and topics.
 */
public class GroupInfoControlActivity extends Activity 
{
	private Spinner pollCombobox;
	private String[] pollTitles;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {   		  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activate_polls_topics);

        //setup comboBox for polls
        this.pollCombobox = (Spinner) findViewById(R.id.actpt_pollCombobox);
        loadPollsIntoSpinnerControl();
        
        LIM_Engine lim = LIM_Engine.getInstance();
        //
        TextView groupInfo = (TextView)findViewById(R.id.actpt_groupInfo_tv);
        groupInfo.setText(lim.getGroupInfo());
        
        //button to open a poll
        Button openPollButton = (Button) findViewById(R.id.actpt_openPollButton);
         
        openPollButton.setOnClickListener( new OnClickListener() {
        	public void onClick(View v)
        	{
        		Object selectedItem = GroupInfoControlActivity.this.pollCombobox.getSelectedItem();
        		
        		if(selectedItem == null) //no item selected
        			return;
        		
        		String selectedPoll = selectedItem.toString();
        		
        		GroupInfoControlActivity.this.onPollOpened(selectedPoll);
        	}
        } );
        
        
        //button for activating next topic
        Button activateTopicButton = (Button) findViewById(R.id.activateTopicButton);
        activateTopicButton.setText(getNextTopic());
        
        activateTopicButton.setOnClickListener( new OnClickListener() {
        	public void onClick(View v)
        	{
        		Button btn = (Button) GroupInfoControlActivity.this.findViewById(R.id.activateTopicButton);
        		GroupInfoControlActivity.this.onActivateTopic(btn.getText().toString());

        		Button activateTopicButton = (Button) findViewById(R.id.activateTopicButton);
        		activateTopicButton.setText(GroupInfoControlActivity.this.getNextTopic());
        	}
        } );
        
        //close button
        View closeButton = findViewById(R.id.actpt_closeButton);
        closeButton.setOnClickListener( new OnClickListener() {
        	public void onClick(View v)
        	{
        		GroupInfoControlActivity.this.finish();
        	}
        } );
        
      
        if(!lim.isMaster()) //client or history cannot activate topics
        {
        	View activateTopicLabel = findViewById(R.id.activateTopicLabel);
        	activateTopicLabel.setVisibility(View.GONE);
        	activateTopicButton.setVisibility(View.GONE);
        	View activateLabel = findViewById(R.id.activateLabel);
        	activateLabel.setVisibility(View.GONE);
        }
    }
    
    
    private void onPollOpened(String selectedPoll) 
    {
       	Log.d("onPollOpened", "poll: " + selectedPoll);
		Poll currentPoll = Poll.getPoll(selectedPoll);
		if(currentPoll == null)
		{
			Log.d("onPollOpened Error", "poll: " + selectedPoll + " doesn't exist.");
			return;
		}
       
    	if(LIM_Engine.getInstance().isMaster()) //master -> activate poll
        {
    		// LIM_Engine: open poll + make sure it cannot be opened twice (ignore then)
			if(!currentPoll.isActive())
    		{
				// open Poll activates the poll
				LIM_Engine.getInstance().master_OpenPoll(selectedPoll);
    		}

			//start activity to show poll results
	        Intent intent = new Intent(Intent.ACTION_VIEW);
	        intent.setClassName(GroupInfoControlActivity.this, PollResultsActivity.class.getName());
	        intent.putExtra("PollTitle", selectedPoll);
	        startActivity(intent);  
        } else {
        	// if poll is still active => open poll answer, else poll results
        	if ((currentPoll.isActive()) && (LIM_Engine.getInstance().isClient()))
        	{
				//show activity to answer poll
				Intent intent = new Intent(Intent.ACTION_VIEW);
		        intent.setClassName(GroupInfoControlActivity.this, AnswerPollActivity.class.getName());
		        intent.putExtra("PollTitle", selectedPoll);
		        startActivity(intent);
        	} else {
        		//start activity to show poll results
    	        Intent intent = new Intent(Intent.ACTION_VIEW);
    	        intent.setClassName(GroupInfoControlActivity.this, PollResultsActivity.class.getName());
    	        intent.putExtra("PollTitle", selectedPoll);
    	        startActivity(intent);  
        	}
        }
    	
	}

	private String getNextTopic() 
    {
		Topic lastTopic = LIM_Engine.getInstance().master_LastTopicStamp();
		Topic t2 = null;
		if (lastTopic == null)
		{
			t2 = Topic.getFirstTopic();
		} else {
			t2 = Topic.getTopicAfter(LIM_Engine.getInstance().master_LastTopicStamp());
		}
		if (t2 != null)
		{
			return t2.getTopicText();
		} else { 
			return "#End#";
		}
	}


	private void onActivateTopic(String topicTitle)
    {
    	Log.d("Topic actived", "Topic: " + topicTitle);
    	Topic t = Topic.getTopic(topicTitle);
    	if (t != null)
    	{
    		LIM_Engine.getInstance().master_SendTopicStamp(t);
    	}
    	
    	this.finish();
    }
    	
      
    /*
     * Adds available polls to spinner control
     * */
	private void loadPollsIntoSpinnerControl() 
	{
		ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	
		pollTitles = Poll.getAllPollTitles();
    
    	if(pollTitles != null)
    	{
    		for(String currentPoll : pollTitles)
    			adapter.add(currentPoll);
    	}
    	
    	this.pollCombobox.setAdapter(adapter);
	}
}
